/**
 * Utility class. Holds the most used methods. 
 * 
 * @author Alican Ozgoren
 * @version 12/14/2012
 */

package edu.brockport.shuttleapp;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

import android.content.Context;
import android.widget.Toast;

public class Utilities 
{
	// Preference file name
	public static final String PREFS_NAME = "Preferences";
	
	// Error codes
	public static final int NO_ERROR = 0;
	public static final int UNEXPECTED_ERROR = 1;
	public static final int NO_SHUTTLE_AVAILABLE = 2;
	public static final int EQUAL_SELECTED_STOPS = 3;
	public static final int EMPTY_QUERY_RESULT = 4;
	public static final int LOCATION_DISABLED = 5;
	
	//----------------------------------------------------------
	public static void toaster(Context context, String msg)
    {
    	Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
	
	//----------------------------------------------------------
	public static void longToaster(Context context, String msg)
    {
    	Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
	
	//----------------------------------------------------------
	public static MyResult convertDBTimeTo12HourFormat(Context myContext, String dbTimeFormat)
	{
		MyResult result = null;
		DateFormat timeFormat = new SimpleDateFormat("HHmm");
		Calendar myCalendar = Calendar.getInstance();
		Date date = null;
		try 
		{
			date = timeFormat.parse(dbTimeFormat);
			myCalendar.setTime(date);
		} 
		catch (ParseException e) 
		{
			result = new MyResult(UNEXPECTED_ERROR, null, "Unexpected error!");
			return result;
		}
		
		DateFormat convertedTimeFormat = new SimpleDateFormat("hh:mm aaa");
		Vector resultVector = new Vector();
		resultVector.add(convertedTimeFormat.format(myCalendar.getTime()));
		result = new MyResult(NO_ERROR, resultVector, null);
		
		return result;
	}
	
	//----------------------------------------------------------
	public static Properties getDateLabelTimeInDBFormat(String labelTime) throws ParseException
	{
		DateFormat dateFormat = 
				new SimpleDateFormat("EEE, MMM d, ''yy 'at' hh:mm aaa");
		Date labelDate = dateFormat.parse(labelTime);
		Calendar myCal = Calendar.getInstance();
		myCal.setTime(labelDate);
		
		DateFormat dbTimeFormat = new SimpleDateFormat("HHmm");
		DateFormat dbDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Properties timeAndDate = new Properties();
		timeAndDate.setProperty("Date", dbDateFormat.format(myCal.getTime()));
		timeAndDate.setProperty("Time", dbTimeFormat.format(myCal.getTime()));
		
		return timeAndDate;
	}
	
	//----------------------------------------------------------
	public static int parseAppTimeToDBTime(String appTime) throws ParseException
	{
		Calendar myCal = Calendar.getInstance();
		DateFormat appTimeFormat = new SimpleDateFormat("HHmm");
		myCal.setTime(appTimeFormat.parse(appTime));
		myCal.add(myCal.HOUR_OF_DAY, -6);
		
		DateFormat dbTimeFormat = new SimpleDateFormat("HHmm");
		String dbTime = dbTimeFormat.format(myCal.getTime());
		
		return Integer.parseInt(dbTime);
	}
	
	//----------------------------------------------------------
	public static Vector<Properties> convertDBTimeToAppTime(Vector<Properties> runs) throws ParseException
	{
		for(Properties row: runs)
		{
			String departure = row.getProperty("DepartureTime");
			String arrival = row.getProperty("ArrivalTime");
			
			int ideparture = Integer.parseInt(departure) + 600;
			int iarrival = Integer.parseInt(arrival) + 600;
			departure = addLeadingZeros(ideparture);
			arrival = addLeadingZeros(iarrival);
			
			row.setProperty("DepartureTime", departure);
			row.setProperty("ArrivalTime", arrival);
		}
		
		return runs;
	}
	
	//----------------------------------------------------------
	private static String addLeadingZeros(int num) 
	{
	    char[] zeros = new char[4];
	    Arrays.fill(zeros, '0');
	    DecimalFormat df = new DecimalFormat(String.valueOf(zeros));
	    
	    return df.format(num);
	}
}
